package com.example.beautyhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.beautyhub.model.Customers;
import com.example.beautyhub.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/customer")
@Api(tags = "客户")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmployeeController employeeController;

    @GetMapping("/")
    public List<Customers> list() {
        return customerService.list();
    }

//    @ApiOperation("搜索")
//    @GetMapping("/search")
//    public List<Customers> getSearchList(@RequestBody Customers customers) {
//        QueryWrapper<Customers> wrapper = new QueryWrapper<>();
//        wrapper.like("name",customers.getName()); //根据条件查询
//        return customerService.list(wrapper);//返回分页数据
//    }

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Page<Customers> getEmployeeList(int pageNum, int pageSize,Customers customers) {
        Page<Customers> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Customers> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(customers.getName())){
            wrapper.like("name",customers.getName()); //根据条件查询
        }
        page.setTotal(customerService.count(wrapper));
        return customerService.page(page,wrapper);//返回分页数据
    }

    @GetMapping("/{id}")
    public Customers findById(@PathVariable Integer id) {
        return customerService.getById(id);
    }

//    @ApiOperation("新增客户，需要传入员工id 作为负责人")
    @PostMapping("/")
    public void add(@RequestBody Customers customer) {
        customer.setCreatedate(new Date());
//        customer.setEmployeesId(id);
//        customer.setEmployeesName(employeeController.findById(id).getName());
        customerService.save(customer);
    }

    @ApiOperation("修改员工负责人")
    @PutMapping("/employee/{id}")
    public void updateEmployees(@PathVariable Integer id, @RequestBody Customers customer) {
        customer.setEmployeesId(id);
        customer.setEmployeesName(employeeController.findById(id).getName());
        customerService.updateById(customer);
    }

    @ApiOperation("修改客户信息")
    @PutMapping("/{id}")
    public void update(@PathVariable Integer id, @RequestBody Customers customer) {
        customer.setId(id);
        customerService.updateById(customer);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        customerService.removeById(id);
    }
}

